package com.zenithmind.classroom.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.classroom.pojo.dto.LessonCreateDTO;
import com.zenithmind.classroom.pojo.dto.LessonUpdateDTO;
import com.zenithmind.classroom.pojo.dto.LessonQueryDTO;
import com.zenithmind.classroom.pojo.vo.LessonDetailVO;
import com.zenithmind.classroom.pojo.vo.LessonVO;
import com.zenithmind.classroom.service.CourseLessonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程课时控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "课程课时管理", description = "课程课时管理相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/lessons")
@RequiredArgsConstructor
public class CourseLessonController {

    private final CourseLessonService courseLessonService;

    @Operation(summary = "创建课时", description = "创建新的课程课时")
    @PostMapping
    public Result<LessonVO> createLesson(@Valid @RequestBody LessonCreateDTO createDTO) {
        LessonVO lesson = courseLessonService.createLesson(createDTO);
        return Result.success(lesson);
    }

    @Operation(summary = "更新课时", description = "更新课时信息")
    @PutMapping("/{id}")
    public Result<Boolean> updateLesson(
            @Parameter(description = "课时ID") @PathVariable String id,
            @Valid @RequestBody LessonUpdateDTO updateDTO) {
        Boolean result = courseLessonService.updateLesson(id, updateDTO);
        return Result.success(result);
    }

    @Operation(summary = "删除课时", description = "删除指定课时")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteLesson(@Parameter(description = "课时ID") @PathVariable String id) {
        Boolean result = courseLessonService.deleteLesson(id);
        return Result.success(result);
    }

    @Operation(summary = "发布课时", description = "发布指定课时")
    @PostMapping("/{id}/publish")
    public Result<Boolean> publishLesson(@Parameter(description = "课时ID") @PathVariable String id) {
        Boolean result = courseLessonService.publishLesson(id);
        return Result.success(result);
    }

    @Operation(summary = "下架课时", description = "下架指定课时")
    @PostMapping("/{id}/unpublish")
    public Result<Boolean> unpublishLesson(@Parameter(description = "课时ID") @PathVariable String id) {
        Boolean result = courseLessonService.unpublishLesson(id);
        return Result.success(result);
    }

    @Operation(summary = "分页查询课时", description = "分页查询课时列表")
    @GetMapping("/page")
    public Result<PageResult<LessonVO>> getLessonPage(@Valid LessonQueryDTO queryDTO) {
        PageResult<LessonVO> result = courseLessonService.getLessonPage(queryDTO);
        return Result.success(result);
    }

    @Operation(summary = "获取章节课时列表", description = "获取指定章节的课时列表")
    @GetMapping("/chapter/{chapterId}")
    public Result<List<LessonVO>> getChapterLessons(
            @Parameter(description = "章节ID") @PathVariable String chapterId,
            @Parameter(description = "是否包含未发布") @RequestParam(defaultValue = "false") Boolean includeUnpublished) {
        String userId = UserContext.getUserId();
        List<LessonVO> lessons = courseLessonService.getChapterLessons(chapterId, userId, includeUnpublished);
        return Result.success(lessons);
    }

    @Operation(summary = "获取课程课时列表", description = "获取指定课程的课时列表")
    @GetMapping("/course/{courseId}")
    public Result<List<LessonVO>> getCourseLessons(
            @Parameter(description = "课程ID") @PathVariable String courseId,
            @Parameter(description = "是否包含未发布") @RequestParam(defaultValue = "false") Boolean includeUnpublished) {
        String userId = UserContext.getUserId();
        List<LessonVO> lessons = courseLessonService.getCourseLessons(courseId, userId, includeUnpublished);
        return Result.success(lessons);
    }

    @Operation(summary = "获取课时详情", description = "获取课时详细信息")
    @GetMapping("/{id}")
    public Result<LessonDetailVO> getLessonDetail(@Parameter(description = "课时ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        LessonDetailVO lesson = courseLessonService.getLessonDetail(id, userId);
        return Result.success(lesson);
    }

    @Operation(summary = "获取课时播放地址", description = "获取课时播放地址")
    @GetMapping("/{id}/play-url")
    public Result<String> getLessonPlayUrl(@Parameter(description = "课时ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        String playUrl = courseLessonService.getLessonPlayUrl(id, userId);
        return Result.success(playUrl);
    }

    @Operation(summary = "调整课时顺序", description = "调整课时排序")
    @PostMapping("/reorder")
    public Result<Boolean> reorderLessons(
            @Parameter(description = "章节ID") @RequestParam String chapterId,
            @Parameter(description = "课时ID列表") @RequestBody List<String> lessonIds) {
        Boolean result = courseLessonService.reorderLessons(chapterId, lessonIds);
        return Result.success(result);
    }

    @Operation(summary = "移动课时", description = "移动课时到其他章节")
    @PostMapping("/{id}/move")
    public Result<Boolean> moveLesson(
            @Parameter(description = "课时ID") @PathVariable String id,
            @Parameter(description = "目标章节ID") @RequestParam String targetChapterId,
            @Parameter(description = "新排序值") @RequestParam Integer sortOrder) {
        Boolean result = courseLessonService.moveLesson(id, targetChapterId, sortOrder);
        return Result.success(result);
    }

    @Operation(summary = "复制课时", description = "复制现有课时")
    @PostMapping("/{id}/copy")
    public Result<LessonVO> copyLesson(
            @Parameter(description = "课时ID") @PathVariable String id,
            @Parameter(description = "目标章节ID") @RequestParam String targetChapterId,
            @Parameter(description = "新课时标题") @RequestParam String title) {
        LessonVO lesson = courseLessonService.copyLesson(id, targetChapterId, title);
        return Result.success(lesson);
    }

    @Operation(summary = "上传课时资源", description = "上传课时资源文件")
    @PostMapping("/{id}/upload")
    public Result<Boolean> uploadLessonResource(
            @Parameter(description = "课时ID") @PathVariable String id,
            @Parameter(description = "资源类型") @RequestParam String resourceType,
            @Parameter(description = "资源URL") @RequestParam String resourceUrl,
            @Parameter(description = "资源大小") @RequestParam Long resourceSize) {
        Boolean result = courseLessonService.uploadLessonResource(id, resourceType, resourceUrl, resourceSize);
        return Result.success(result);
    }

    @Operation(summary = "记录课时观看", description = "记录课时观看进度")
    @PostMapping("/{id}/watch")
    public Result<Boolean> recordLessonWatch(
            @Parameter(description = "课时ID") @PathVariable String id,
            @Parameter(description = "观看时长") @RequestParam Integer watchTime,
            @Parameter(description = "观看位置") @RequestParam Integer position) {
        String userId = UserContext.getUserId();
        Boolean result = courseLessonService.recordLessonWatch(id, userId, watchTime, position);
        return Result.success(result);
    }

    @Operation(summary = "完成课时学习", description = "标记课时学习完成")
    @PostMapping("/{id}/complete")
    public Result<Boolean> completeLessonStudy(@Parameter(description = "课时ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        Boolean result = courseLessonService.completeLessonStudy(id, userId);
        return Result.success(result);
    }

    @Operation(summary = "获取下一个课时", description = "获取下一个课时")
    @GetMapping("/{id}/next")
    public Result<LessonVO> getNextLesson(@Parameter(description = "当前课时ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        LessonVO lesson = courseLessonService.getNextLesson(id, userId);
        return Result.success(lesson);
    }

    @Operation(summary = "获取上一个课时", description = "获取上一个课时")
    @GetMapping("/{id}/previous")
    public Result<LessonVO> getPreviousLesson(@Parameter(description = "当前课时ID") @PathVariable String id) {
        String userId = UserContext.getUserId();
        LessonVO lesson = courseLessonService.getPreviousLesson(id, userId);
        return Result.success(lesson);
    }

    @Operation(summary = "获取课时统计", description = "获取课时统计信息")
    @GetMapping("/{id}/statistics")
    public Result<Object> getLessonStatistics(@Parameter(description = "课时ID") @PathVariable String id) {
        Object statistics = courseLessonService.getLessonStatistics(id);
        return Result.success(statistics);
    }

    @Operation(summary = "更新课时统计", description = "更新课时统计信息")
    @PostMapping("/{id}/statistics/update")
    public Result<Boolean> updateLessonStatistics(@Parameter(description = "课时ID") @PathVariable String id) {
        Boolean result = courseLessonService.updateLessonStatistics(id);
        return Result.success(result);
    }

    @Operation(summary = "批量导入课时", description = "批量导入课时数据")
    @PostMapping("/batch/import")
    public Result<Integer> batchImportLessons(@Valid @RequestBody List<LessonCreateDTO> lessons) {
        Integer count = courseLessonService.batchImportLessons(lessons);
        return Result.success(count);
    }

    @Operation(summary = "导出课时", description = "导出指定课时数据")
    @PostMapping("/batch/export")
    public Result<List<LessonVO>> exportLessons(@RequestBody List<String> lessonIds) {
        List<LessonVO> lessons = courseLessonService.exportLessons(lessonIds);
        return Result.success(lessons);
    }
}
